<template>
	<el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
		class="demo-ruleForm" :size="formSize" status-icon>
		<el-form-item label="客户名称" prop="ClientName">
			<el-input v-model="ruleForm.ClientName" />
		</el-form-item>
		
		<el-form-item label="地址" prop="Address">
			<el-input v-model="ruleForm.Address" />
		</el-form-item>
		<el-form-item label="电话" prop="Phone">
			<el-input v-model="ruleForm.Phone" />
		</el-form-item>
		<el-form-item label="联系人" prop="Contact">
			<el-input v-model="ruleForm.Contact" />
		</el-form-item>
		<el-form-item label="密码" prop="LoginPwd" >
			<el-input show-password v-model="ruleForm.LoginPwd" />
		</el-form-item>
		
		
		<el-form-item>
			<el-button type="primary" @click="submitForm(ruleFormRef)">
				保存
			</el-button>
			<el-button @click="resetForm(ruleFormRef)">重置</el-button>
		</el-form-item>
	</el-form>
</template>

<script lang="ts" setup>
	import { reactive, ref } from 'vue'
	import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
	import { ElNotification } from 'element-plus'

	import clientapi from "@/api/clientapi"

	const emit = defineEmits(['cloesdrawer'])

	interface RuleForm {
		ClientName : string					
		Address : string	
		Phone:string
		Contact:string
		LoginPwd:string
	}

	const formSize = ref<ComponentSize>('default')
	const ruleFormRef = ref<FormInstance>()
	const ruleForm = reactive<RuleForm>({
		ClientName: '',
		Address:'',
		Phone:'',
		Contact:'',
		LoginPwd:'',
	})
		
	const rules = reactive<FormRules<RuleForm>>({
		 ClientName: [
			{ required: true, message: '请输入角色名称', trigger: 'blur' },
			
		],		
		
	})

	const submitForm = async (formEl : FormInstance | undefined) => {
		if (!formEl) return
		await formEl.validate((valid, fields) => {
			if (valid) {
				clientapi.Add(ruleForm).then(res => {
					if (res.code == 1) {
						ElNotification({
							title: 'Success',
							message: '添加成功',
							type: 'success',
							duration: 800,		
						})
							emit("cloesdrawer", res.data)
					} else {
						ElNotification({
							title: 'Error',
							message: '失败',
							type: 'Error',
							duration: 800,
						})
					}
				}).catch(err => {
					ElNotification({
						title: 'Error',
						message: '失败',
						type: 'Error',
						duration: 800,
					})
				})
			} else {
				console.log('error submit!', fields)
			}
		})
	}

	const resetForm = (formEl : FormInstance | undefined) => {
		if (!formEl) return
		formEl.resetFields()
	}
</script>